package com.hww.sort.impl;

import com.hww.sort.ISortNumber;

/**
 * 采用冒泡發對整型數組進行排序
 * 
 * @author:Huangwenwei
 * @date:2014-6-18
 * @time:下午4:42:48
 */
public class BubbleSort implements ISortNumber {

	public BubbleSort() {
	}

	@Override
	public int[] sortASC(int[] intArray) {
		if (intArray == null)
			return null;
		int[] srcData = (int[]) intArray.clone();
		boolean changePosition = true;
		int comparedTimes = 0;
		int maxCompareTimes = srcData.length - 1;

		while ((comparedTimes < maxCompareTimes && changePosition)) {
			for (int i = 0; i < (maxCompareTimes - comparedTimes); i++) {
				changePosition = false;
				if (srcData[i] > srcData[i + 1]) {
					swap(srcData, i, i + 1);
					changePosition = true;
				}
			}
			comparedTimes++;
		}
		return srcData;
	}

	/**
	 * 
	 * @time:下午5:09:48
	 * @data:2014-6-18
	 * @auto:mr.huangwenwei@gmail.com
	 * @return:void
	 */
	private void swap(int[] srcData, int i, int j) {
		int temp = srcData[i];
		srcData[i] = srcData[j];
		srcData[j] = temp;
	}

}
